﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL.DBHelpers;
namespace QuanLyKhachHang
{
    public partial class UC_QLNV : UserControl
    {
        public UC_QLNV()
        {
            InitializeComponent();
        }
        string Id = string.Empty;
        public void loaddata()
        {
            DataTable dt = DBHelper.GetDataTableSP("sp_User_getAllUser_TenNhomQuyen");
            gdNV.DataSource = dt;
        }
        public void loadquyen()
        {
            DataTable dt = DBHelper.GetDataTableSP("sp_NhomQuyen_getAllNhom");
            cbbQuyen.DataSource = dt;
            cbbQuyen.DisplayMember = "NhomQuyen";
            cbbQuyen.ValueMember = "Id";
        }
        public void resetField()
        {
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtMaNV.Text = "";
            txtNgaySinh.Text= "";
            txtPass.Text = "";
            txtSDT.Text = "";
            txtTenNV.Text = "";
            txtUserName.Text = "";
            txtMaNV.Enabled=true;

        }
        private void UC_QLNV_Load(object sender, EventArgs e)
        {
            loaddata();
            loadquyen();
            
        }

        private void labelControl4_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string a = cbbQuyen.SelectedValue.ToString();
            string MaNV =string.Empty ;
            if (DBHelper.CheckExitInDB(txtMaNV.Text.ToUpper(),"MaNhanVien","sp_User_getAllUser_TenNhomQuyen")==true)
            {
                MessageBox.Show("Mã Nhân viên này đã tồn tại");
                return;
            }
            else
            {
                MaNV = txtMaNV.Text.ToUpper();
            }
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@MaNV",MaNV));
            ls.Add(new SqlParameter("@TenNV",txtTenNV.Text));
            ls.Add(new SqlParameter("@UserName",txtUserName.Text));
            ls.Add(new SqlParameter("@Pass",txtPass.Text));
            ls.Add(new SqlParameter("@NgaySinh",txtNgaySinh.Text));
            ls.Add(new SqlParameter("@Email",txtEmail.Text));
            ls.Add(new SqlParameter("@SDT",txtSDT.Text));
            ls.Add(new SqlParameter("@DiaChi",txtDiaChi.Text));
            ls.Add(new SqlParameter("@NhomQuyenId",a));
            bool TrangThai ;
            if (cbTrangThai.Checked) TrangThai = true;
            else TrangThai =false;
            ls.Add(new SqlParameter("@TrangThai",TrangThai));
            DBHelper.ExecuteNonQuery("sp_User_AddNhanVien",ls);
            loaddata();
            MessageBox.Show("Thêm Thành công !");
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
           
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@MaNhanVien",txtMaNV.Text));
            ls.Add(new SqlParameter("@TenNhanVien",txtTenNV.Text));
            ls.Add(new SqlParameter("@UserName",txtUserName.Text));
            ls.Add(new SqlParameter("@Pass",txtPass.Text));
            ls.Add(new SqlParameter("@NgaySinh",txtNgaySinh.Text));
            ls.Add(new SqlParameter("@Email",txtEmail.Text));
            ls.Add(new SqlParameter("@SDT",txtSDT.Text));
            ls.Add(new SqlParameter("@DiaChi",txtDiaChi.Text));
            
            string b = cbbQuyen.SelectedValue.ToString();
            ls.Add(new SqlParameter("@NhomQuyenId",b));
            Boolean a;
            if (cbTrangThai.Checked) a =true;
            else a = false;
            ls.Add(new SqlParameter("@TrangThai",a));
            DBHelper.ExecuteNonQuery("sp_User_UpdateUser",ls);
            loaddata();
            resetField();
            MessageBox.Show("Cập nhật thành công!");

        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@MaNV",Id));
            DBHelper.ExecuteNonQuery("sp_User_DeleteUser",ls);
            resetField();
            loaddata();
            MessageBox.Show("Xóa thành công !");
        }

        private void BtnPrint_Click(object sender, EventArgs e)
        {
            gdNV.ShowPrintPreview();
        }

        private void BtnExport_Click(object sender, EventArgs e)
        {
            
        }

        private void gdNV_Click(object sender, EventArgs e)
        {
            txtMaNV.Enabled = false;
            Id = gdvNV.GetFocusedDisplayText();

            DataTable dt = new DataTable();
            List<SqlParameter> l = new List<SqlParameter>();
            l.Add(new SqlParameter("@MaNV", Id));
            dt = DBHelper.GetDataTableSP("sp_User_getUserByIdorMaNV", l);
            string NhomQuyenId = dt.Rows[0]["NhomQuyenId"].ToString();
            txtMaNV.Text = gdvNV.GetFocusedRowCellDisplayText(colMaNV);
            txtTenNV.Text = gdvNV.GetFocusedRowCellDisplayText(colTenNV);
            txtUserName.Text = gdvNV.GetFocusedRowCellDisplayText(colUserName);
            txtPass.Text = gdvNV.GetFocusedRowCellDisplayText(colPass);
            txtEmail.Text = gdvNV.GetFocusedRowCellDisplayText(colEmail);
            txtSDT.Text = gdvNV.GetFocusedRowCellDisplayText(colSDT);
            txtDiaChi.Text = gdvNV.GetFocusedRowCellDisplayText(colDiaChi);
            string quyen = gdvNV.GetFocusedRowCellDisplayText(colQuen);
            cbbQuyen.Controls.Clear();
            cbbQuyen.SelectedValue = NhomQuyenId;
            //cbbQuyen.SelectedText = quyen;
            string tt = gdvNV.GetFocusedRowCellDisplayText(colTrangThai);
            if (tt == "Checked") cbTrangThai.Checked = true;
            else
            {
                cbTrangThai.Checked = false;
            }
            txtNgaySinh.Text = gdvNV.GetFocusedRowCellDisplayText(colNgaySinh);
        }
        
    }
}
